package com.ecity.table {
	import com.ecity.events.GameEvent;
	import com.ecity.util.HashMap;
	import flash.accessibility.AccessibilityImplementation;
	import flash.display.Sprite;
	import gs.TweenLite;
	/**
	 * ...
	 * @author ss
	 */
	public class Desktop extends TableDisplayObject	{
		private static const POSI:Array = [[500, 660, 0], [900, 380, -90], [500, 100, 180], [100, 380, 90]];
		
		private var _initpage:Sprite;
		private var _background:Sprite;
		private var _rulepage:Sprite;
		private var _backActive:Sprite;
		private var _backReal:Sprite;
		
		private var cards:Array = [];
		
		public function Desktop(){

		}
		
		public function initPokerData():void {
			for (var i:int = 0; i < 52; i++) {
				cards.push(i);
			}
		}
		
		/**
		 * 显示背景，隐藏开始页面和规则页面
		 */
		public function showBackground():void {
			_initpage = getLayer("initpage");
			_rulepage = getLayer("rulepage");
			TweenLite.to(_rulepage, 1, { alpha:0 } );
			TweenLite.to(_initpage, 1, {delay:1,alpha:0,onComplete:onBackgroundInit});
		}
		/**
		 * 背景图显示后，发送事件，请求添加开始按钮；
		 */
		private function onBackgroundInit():void {
			this.dispatchEvent(new GameEvent(GameEvent.BACKGROUND_INITED));
			initPokerData();
			initCardBack();
		}
		
		private function initCardBack():void {
			_backReal = this.getLayer("back");
			_backActive = this.getLayer("backAct");
		}
		
		public function send(index:int):void {
			var idx:int = Math.floor(cards.length * Math.random());
			var value:int = cards[idx];
			cards.splice(idx, 1);
			if (cards.length < 4) {
				initPokerData();
			}
			_backReal.alpha = 1;
			_backActive.alpha = 1;
			TweenLite.to(_backActive,0.5,{x:POSI[index][0],y:POSI[index][1],rotation:POSI[index][2],onComplete:gotoOriginal,onCompleteParams:[index,value]})
		}
		
		private function gotoOriginal(index:int,value:int):void {
			this.dispatchEvent(new GameEvent(GameEvent.CARD_SENT,{index:index,value:value}));
			_backActive.alpha = 1;
			_backActive.x = 1024 / 2;
			_backActive.y = 768 / 2;
			_backActive.rotation = 0;
		}
		
	}

}